/*
 * @Author: zhangfeng
 * @Date: 2023-12-13 18:18:30
 * @LastEditTime: 2024-01-09 16:29:15
 * @LastEditors: zhangfeng
 * @Description:
 * @FilePath: \nodeapi\src\controller\menus.controller.js
 * © 2023 张峰，All rights reserved.
 */
const {
  createMenus,
  findMenus,
  updateMenus,
  deleteserverMenus,
} = require("../server/menus.server");

const {
  menuCreateFormatterError,
  menuGetFormatterError,
  menuUpdateFormatterError,
  menuDeleteFormatterError,
} = require("../constants/err.type");

class MenusController {
  // 获取菜单列表
  async getMenus(ctx, next) {
    try {
      const res = await findMenus();
      ctx.body = {
        code: 0,
        message: "获取菜单列表成功",
        result: res,
      };
    } catch (e) {
      console.error(e);
      ctx.body = menuGetFormatterError;
    }
  }
  // 创建菜单
  async create(ctx, next) {
    const params = ctx.request.body;
    try {
      const result = await createMenus(params);
      ctx.body = {
        code: 0,
        message: "添加菜单成功",
        result: "",
      };
    } catch (e) {
      console.error(e);
      ctx.body = menuCreateFormatterError;
    }
  }
  // 修改菜单
  async update(ctx, next) {
    const params = ctx.request.body;
    try {
      const result = await updateMenus(params);
      if (result) {
        ctx.body = {
          code: 0,
          message: "修改菜单成功",
          result: result,
        };
      } else {
        ctx.body = menuUpdateFormatterError;
      }
    } catch (e) {
      console.error(e);
      ctx.body = menuUpdateFormatterError;
    }
  }
  // 删除菜单
  async deleteMenus(ctx, next) {
    const { id } = ctx.request.body;
    try {
      const result = await deleteserverMenus(id);
      if (result) {
        ctx.body = {
          code: 0,
          message: "删除菜单成功",
          result: "",
        };
      } else {
        ctx.body = menuDeleteFormatterError;
      }
    } catch (e) {
      console.error(e);
      ctx.body = menuDeleteFormatterError;
    }
  }
}

module.exports = new MenusController();
